package com.example.card.transactionimplementation;

import com.example.card.CardConstant;
import com.example.card.CardDataBase;
import com.example.card.abstracttransactions.CardConsumptionTransaction;
import com.example.card.domain.card.Card;
import com.example.card.domain.order.AddOilOrder;
import com.example.card.implemention.PayCheck;

public class CardAddOilTransaction extends CardConsumptionTransaction {


    private final int oilType;

    public CardAddOilTransaction(int memberId, int id, int amount, int oilType) {
        super(memberId, id, amount);
        this.oilType = oilType;
    }

    @Override
    public void consumer(Card card) {
        PayCheck payCheck = new PayCheck(amount);

        checkOilType(card.getOilLabel());

        payCheck.setOilCoinDeduct(user.deductOilCoin(amount));

        payCheck.setvOilDeduct(user.deductVOil(amount));

        card.pay(payCheck);

        AddOilOrder addOilOrder = new AddOilOrder(memberId, card.getId(), card.getCardNo(), amount
                , payCheck.getActualPay(), payCheck.getOilCoinDeduct(), payCheck.getvOilDeduct());

        CardDataBase.saveAddOilOrder(addOilOrder);
    }

    private void checkOilType(int cardOilType) {

        if ((oilType & cardOilType) != oilType) {
            throw new RuntimeException("改卡不支持此油品标号");
        }
    }


}
